function throttle(fn, interval = 400, { leading = false, trailing = true } = {}) {
  let startTime = 0
  let timer = null
  const _throttled = function(...args) {
    const newTime = new Date().getTime()

    
    if (!startTime && !leading) startTime = newTime

    const waitTime = interval - (newTime - startTime)

    if (waitTime <= 0) {
      if (timer) {
        clearTimeout(timer)
        timer = null
      }

      fn.apply(this, args)
      startTime = newTime
      return
    }

    if (trailing && !timer) {
      timer = setTimeout(() => {
        fn.apply(this, args)
        startTime = new Date().getTime()
        timer = null
      }, waitTime)
    }
  }

  return _throttled
}